package com.Hermes.User;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

public class Floyd_Warshall {

    private final int[][] graph;
    private final int[][] pathlist;
    private final int LEN;
    private final PD_oracle pd_oracle;
    private final ArrayList<node> apl;//节点数组
    private final ConcurrentHashMap<Long, Integer> Id2Index;


    public Floyd_Warshall(ArrayList<node> apl, PD_oracle pdOracle, ConcurrentHashMap<Long, Integer> i2i) {
        this.apl = apl;
        this.LEN = apl.size();
        graph = new int[LEN + 9][LEN + 9];
        pathlist = new int[LEN + 9][LEN + 9];
        pd_oracle = pdOracle;
        Id2Index = i2i;

    }


    public int[][] getGraph() {
        return graph;
    }

    public int[][] getPathlist() {
        return pathlist;
    }


    public PD_oracle getPd_oracle() {
        return pd_oracle;
    }

    public void setup() {
        for (int i = 0; i < apl.size() + 3; i++) {
            for (int j = 0; j < apl.size() + 3; j++) {
                graph[i][j] = 200000000;
                pathlist[i][j] = j;
            }//初始化为最大值
            graph[i][i] = 0;
        }
        for (int i = 0; i < LEN; i++) {
            node ni = apl.get(i);
            int from = ni.index;
            for (int j = 0; j < ni.Neb.size(); j++) {
                int to = ni.Neb.get(j);
                graph[from][to] = ni.Dis.get(j);
                graph[to][from] = ni.Dis.get(j);
                pathlist[from][to] = to;



            }
        }
//        for (int i = 0; i < apl.size(); i++) {
//            for (int j = 0; j < apl.size(); j++) {
//
//                }
//                //pathlist.put(new id_id(id1,id2),id2);//设置路径数组
//            }
//        }
    }


    /*
     * 计算
     * */
    public void compute() {

        for (int k = 0; k < LEN; k++) {
            for (int i = 0; i < LEN; i++) {
                for (int j = 0; j < LEN; j++) {
                    if (graph[i][j] > (graph[i][k] + graph[k][j])) {
                        graph[i][j] = (graph[i][k] + graph[k][j]);
                        pathlist[i][j] = pathlist[i][k];
                    }
                }
            }
        }

    }

    private void getnode(int i, int j) {//递归添加路径
//        if (pathlist[i][j] == -1 ) return;
//        getnode(i, pathlist[i][j], s, e);
//        pd_oracle.add(s, e, pathlist[i][j]);
//        getnode(pathlist[i][j], j, s, e);
        int k = pathlist[i][j];
        while (k != j) {
            pd_oracle.add(i, j, k);
            k = pathlist[k][j];
        }
    }

    public void make_pdo() {//没写完
        for (int i = 0; i < apl.size(); i++) {
            for (int j = 0; j < apl.size(); j++) {
                pd_oracle.setup(i, j, graph[i][j], LEN);
                if (i == j) {
                    continue;
                }
                getnode(i, j);

            }
        }
    }


}
